﻿var jsonParam = null;
$(function () {
    if (listDataService == null || listDataService == '') {
        listDataService = "TreeListSelectService";
    }
    /*********展示树型结构数据*********/
    jsonParam = convertStringToJson(params)
    if (treeRootText == '' || treeRootText == null) {
        treeRootText = "组织机构";
    }

    if (treeRootId == '' || treeRootId == null) {
        treeRootId = "root";
    }

    if (isTreeAsyn != null && isTreeAsyn == 'true') {
        isTreeAsyn = "true";
    }
    else {
        isTreeAsyn = "false";
    }

    var service_params = {};

    if (treeChildSqlId != '' && treeChildSqlId != null) {
        service_params["sqlId"] = treeRootSqlId;
        service_params["childSqlId"] = treeChildSqlId;
    }
    else {
        service_params["sqlId"] = treeChildSqlId;
    }

    var service_params = jQuery.extend(service_params, jsonParam);

    $('#treeDiv').tree({//tree为容器ID
        asyn: isTreeAsyn, //异步
        rootId: treeRootId,
        rootText: treeRootText,
        CommandName: 'java.tree.command',
        onNodeClick: showList,
        params: service_params
    });

    userTreeObject.setUserListPage(window.frames["userList"]); //获取 用户列表的页面对象
    userTreeObject.setRecordSize(document.getElementById("pageSize").value);
    userTreeObject.setCurrentPage(1);

    listUsers();
});

function getParams() {
    return jsonParam;
}

function convertStringToJson(strings) {
    var jsonParameter = {};
    var strs = strings.split(",");
    for (var i = 0; i < strs.length; i++) {
        var ps = strs[i].split(":");
        jsonParameter["\"" + ps[0] + "\""] = ps[1];
    }

    return jsonParameter;
}

function listUsers() {
    var para = setParam(true);
    document.getElementById("textfield").value = 1;
    if (document.getElementById("page_" + userTreeObject.getCurrentPage()) != null) document.getElementById("page_" + userTreeObject.getCurrentPage()).className = "";
    if (document.getElementById("page_" + 1) != null) document.getElementById("page_" + 1).className = "corrent";
    resetPageList(1);

    var currPage = userTreeObject.getCurrentPage();
    $.dataservice(listDataService + "$GetPageInfo", { sqlId: listSqlId, parameters: para }, function (res) {
        if (res != null && res != '') {
            var pageSize = document.getElementById("pageSize").value;
            if (document.getElementById("pageNum").innerText != Math.ceil(res / pageSize)) {
                document.getElementById("pageNum").innerText = Math.ceil(res / pageSize);
                resetPageList(1);
            }
            if (document.getElementById("page_" + currPage) != null) document.getElementById("page_" + currPage).className = "corrent";
            document.getElementById("recordNum").innerText = res;
        } else {
            document.getElementById("pageNum").innerText = "0";
            document.getElementById("recordNum").innerText = "0";
        }

    }, { async: false });
    var url = "ListSelect.aspx?listSqlId=" + listSqlId + "&service=" + saveService + "&params=" + para + "&selectMode=" + getParams()["\"selectMode\""];
    
    window.frames["userList"].location = url;
}

//点击组织树节点  触发的事件
function showList(id, name, record, currentNode) {
    if (!id) {
        userTreeObject.setSelectedOrg('root');
    }
    userTreeObject.setSelectedOrg(id);
    listUsers();
}

//查询主体函数： 点击查询按钮 触发的事件
function queryUsers(query) {
    if (query) {
        userTreeObject.setCurrentPage(1);
    }
    var currPage = userTreeObject.getCurrentPage();
    var para = setParam(false);
    //alert(para);
    var userName = document.getElementById("USER_NAME");
    para += ",NAME:'" + userName.value + "'";
    var account = document.getElementById("ACCOUNT");
    para += ",ACCOUNT:'" + account.value + "'";
    var mobile = document.getElementById("MOBILE");
    para += ",MOBILE:'" + mobile.value + "'";
    $.dataservice(listDataService + "$GetPageInfo", { sqlId: listSqlId, parameters: para }, function (res) {
        if (res != null && res != '') {
            if (query) {
                document.getElementById("textfield").value = 1;
                if (document.getElementById("page_" + userTreeObject.getCurrentPage()) != null) document.getElementById("page_" + userTreeObject.getCurrentPage()).className = "";
                if (document.getElementById("page_" + 1) != null) document.getElementById("page_" + 1).className = "corrent";
                resetPageList(1);
            }
            var pageSize = document.getElementById("pageSize").value;
            if (document.getElementById("pageNum").innerText != Math.ceil(res / pageSize)) {
                document.getElementById("pageNum").innerText = Math.ceil(res / pageSize);
                resetPageList(1);
            }
            if (document.getElementById("page_" + currPage) != null) document.getElementById("page_" + currPage).className = "corrent";
            document.getElementById("recordNum").innerText = res;
        } else {
            document.getElementById("pageNum").innerText = "0";
            document.getElementById("recordNum").innerText = "0";
        }

    }, { async: false });

    var url = "ListSelect.aspx?listSqlId=" + listSqlId + "&service=" + saveService + "&params=" + para;
    
    window.frames["userList"].location = url;
}

//获取 选择的参数
function setParam(fromTreeNode) {
    var tem = "";
    if (userTreeObject.getSelectedOrg() != '' && userTreeObject.getSelectedOrg() != undefined && userTreeObject.getSelectedOrg() != 'root') {
        tem = "ORG:" + userTreeObject.getSelectedOrg();
    }
  
    if (fromTreeNode) {
        userTreeObject.setCurrentPage(1);
    }

    if (tem == "") {
        tem = "currentPage:" + userTreeObject.getCurrentPage();
    }
    else {
        tem += ",currentPage:" + userTreeObject.getCurrentPage();
    }

    tem += ",pageSize:" + document.getElementById("pageSize").value;
    if (params != null && params != '')
        tem += "," + params;
    return tem;
}

function returnAction(obj) {//返回[values(用","分隔),text(用","分隔)]
    var chooseObj = userTreeObject.submitSelect(obj);
    if (chooseObj != 'undefined' && chooseObj.length > 1) {
        window.returnValue = chooseObj;
        window.close();
    } else {
        alert("没有用户被选中");
        return;
    }

}

/**
*	 重置页数列表 , 对于参数v
*  1) 如果为空或首页, : 判断总页数p是否>9, 如果不大于9, 则直接显示1 至 p;
*											如果大于9, 则显现 1 至 9, 后面加上...
*  2) 如果为-1或总页数p, 则表示显示最后一页: 判断总页数p是否>9, 如果不大于9, 则直接显示1 至 p;
*											如果大于9, 则显现 p-8 至 9, 前面加上...
*  3) 如果是数字n, 判断 1<n<p, 如果	是8或9的倍数, 则进行重绘:
i. 若 n+8>p, 则显示 n 至 n+8, 后面加上...
ii. 否则, 显示n 至 p										
*/
function resetPageList(v) {
    var divHtml = "";
    var pageNum = document.getElementById("pageNum").innerText;
    if (pageNum <= 5) {
        for (var k = 1; k <= pageNum; k++)
            divHtml += '<a href="#" id="page_' + k + '"  onClick="forwordPage(' + k + ', false)"><span>' + k + '</span> </a>';
    } else if (v == null || v <= 1) {
        userTreeObject.minAhref = 0;
        for (var k = 1; k <= 5; k++)
            divHtml += '<a href="#" id="page_' + k + '"  onClick="forwordPage(' + k + ', false)"><span>' + k + '</span> </a>';
        divHtml += '<a href="#" id="page_5"  onClick="forwordPage(6, true)"><span>>></span> </a>';
        userTreeObject.maxAhref = 5;
    } else if (v == pageNum || v + 4 >= pageNum) {
        var pa = pageNum - 5;
        userTreeObject.minAhref = pageNum - 3;
        divHtml += '<a href="#" id="page_' + pa + '"  onClick="forwordPage(' + pa + ', true)"><span>...</span> </a>';
        for (var k = pageNum - 4; k <= pageNum; k++)
            divHtml += '<a href="#" id="page_' + k + '"  onClick="forwordPage(' + k + ', false)"><span>' + k + '</span> </a>';
        userTreeObject.maxAhref = pageNum;
    } else {
        var v1 = v - 1;
        userTreeObject.minAhref = v + 1;
        divHtml += '<a href="#" id="page_' + v1 + '"  onClick="forwordPage(' + v1 + ', true)"><span><<</span> </a>';
        for (var k = v; k <= v + 4; k++)
            divHtml += '<a href="#" id="page_' + k + '"  onClick="forwordPage(' + k + ', false)"><span>' + k + '</span> </a>';
        userTreeObject.maxAhref = v + 3;
        v = v + 5;
        divHtml += '<a href="#" id="page_' + v + '"  onClick="forwordPage(' + v + ', true)"><span>...</span> </a>';
    }
    document.getElementById("pageList").innerHTML = divHtml;
}

//转向指定页面
function forwordPage(p, r) {
    document.getElementById("textfield").value = p;
    if (p >= userTreeObject.maxAhref) resetPageList(p);
    else if (p <= userTreeObject.minAhref) {
        p = p - 4;
        resetPageList(p);
    }
    gotoPage();
}

/**
* 点击'GO'  触发的事件
*  1) 需要重设当前所在页为首页
*  2) 将页数列表重置
*/
function go(page) {
    if (!isDigit(page)) return;
    page = parseInt(page);
    var pageNum = parseInt(document.getElementById("pageNum"));
    if (page > pageNum) page = pageNum;
    forwordPage(page);
}

function gotoPage() {
    var pa = document.getElementById("textfield").value;
    if (document.getElementById("page_" + userTreeObject.getCurrentPage()) != null) document.getElementById("page_" + userTreeObject.getCurrentPage()).className = "";

    if (!isDigit(pa)) return;
    userTreeObject.setCurrentPage(pa);
    if (document.getElementById("page_" + pa) != null) document.getElementById("page_" + pa).className = "corrent";
    queryUsers();
}

/**
* 重设页面大小 或 点击‘首页’ 触发事件
*  1) 需要重设当前所在页为首页
*  2) 将页数列表重置
*/
function pageSizeChangeAction() {
    document.getElementById("textfield").value = 1;
    gotoPage();
}

//重置按钮 单击触发事件
function clearParam() {
    document.getElementById("USER_NAME").value = "";
    document.getElementById("ACCOUNT").value = "";
    document.getElementById("MOBILE").value = "";
}

function checkAllElement(checked) {
    userTreeObject.changeAll(document.getElementById('selectedPerson'), checked);
}

function checkSelection(objId) {
    var obj = document.getElementById(objId);
    obj.checked = !obj.checked;
    if (objId == 'param1')
        checkChange(obj);
}

function checkChange(obj) {
    if (!obj.checked) {
        document.getElementById('param2').checked = false;
    }
}